package com.ljw.ebay.api.find;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ebay.services.finding.FindItemsByKeywordsRequest;
import com.ebay.services.finding.FindItemsByKeywordsResponse;
import com.ebay.services.finding.FindingServicePortType;
import com.ebay.services.finding.GetHistogramsRequest;
import com.ebay.services.finding.GetHistogramsResponse;
import com.ebay.services.finding.GetVersionRequest;
import com.ebay.services.finding.GetVersionResponse;
import com.ebay.services.finding.OutputSelectorType;
import com.ebay.services.finding.PaginationInput;
import com.ebay.services.finding.SearchItem;
import com.ebay.services.finding.SortOrderType;

@Service("ebayFindService")
public class EbayServiceImpl implements EbayFindService {

	private static final Logger logger = Logger
			.getLogger(EbayFindService.class);
	private static final FindingServicePortType findServiceClient = APIServiceClientFactory
			.getFindServiceClient();

	@Override
	public String findItemsByKeywords() {

		FindItemsByKeywordsRequest request = new FindItemsByKeywordsRequest();
		request.setSortOrder(SortOrderType.PRICE_PLUS_SHIPPING_HIGHEST);
		request.setKeywords("hulusi");
		request.setSortOrder(SortOrderType.START_TIME_NEWEST);
		/*
		 * AspectFilter aspectFilter = new AspectFilter();
		 * aspectFilter.setAspectName("outputSelector");
		 * aspectFilter.getAspectValueName().add("AspectHistogram");
		 * request.getAspectFilter().add(aspectFilter);
		 */
		PaginationInput paginationInput = new PaginationInput();
		paginationInput.setEntriesPerPage(20);
		paginationInput.setPageNumber(2);
		request.setPaginationInput(paginationInput);
		request.getOutputSelector().add(OutputSelectorType.ASPECT_HISTOGRAM);
		request.getOutputSelector().add(OutputSelectorType.SELLER_INFO);
		request.getOutputSelector().add(OutputSelectorType.STORE_INFO);
		FindItemsByKeywordsResponse response = findServiceClient
				.findItemsByKeywords(request);
		logger.info("ACK=" + response.getAck());
		logger.info("Find " + response.getSearchResult().getCount() + " items.");
		List<SearchItem> searchedItems = response.getSearchResult().getItem();

		for (SearchItem item : searchedItems) {
			logger.info("Country: " + item.getCountry() + " Title: "
					+ item.getTitle());
		}
		// FIXME ebayapi
		return null;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.qianpin.api.ebay.find.EbayFindService#findItemByKeywords(java.lang
	 * .String)
	 */
	@Override
	public FindItemsByKeywordsResponse findItemByKeywords(String keyword) {
		FindingServicePortType findingService = APIServiceClientFactory
				.getFindServiceClient();
		FindItemsByKeywordsRequest findRequest = new FindItemsByKeywordsRequest();
		findRequest.setKeywords(keyword);
		findRequest.getOutputSelector().add(OutputSelectorType.STORE_INFO);
		findRequest.getOutputSelector().add(OutputSelectorType.SELLER_INFO);
		PaginationInput pginput = new PaginationInput();
		pginput.setEntriesPerPage(5);
		findRequest.setPaginationInput(pginput);

		FindItemsByKeywordsResponse responseItems = findingService
				.findItemsByKeywords(findRequest);
		return responseItems;
	}

	@Override
	public GetHistogramsResponse getHistogram(String categoryid) {
		GetHistogramsRequest histogramsRequest = new GetHistogramsRequest();
		histogramsRequest.setCategoryId(categoryid);
		GetHistogramsResponse histogramsResponse = findServiceClient
				.getHistograms(histogramsRequest);
		return histogramsResponse;
	}

	@Override
	public GetVersionResponse getAPIVersion() {
		FindingServicePortType findingService = APIServiceClientFactory
				.getFindServiceClient();
		return findingService.getVersion(new GetVersionRequest());
	}

}
